<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/14
 * Time: 13:13
 */

namespace App\Models\exams;


use App\Models\exams\observe\ExamObserve;
use App\Models\Model;

class Exam extends Model
{

    protected $fillable = [
        'title',
        'status',
        'cover_img',
        'intro',
        'start_time',
        'end_time',
        'minute',
        'type',
        'group_exam'
    ];

    protected $casts = [
        'group_exam' => 'json',
    ];

    public function getGroupExamAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setGroupExamAttribute($value)
    {
        $this->attributes['group_exam'] = json_encode(array_values($value));
    }

    public static function boot()
    {
        parent::boot();
        static::observe(new ExamObserve());

    }

    public static function checkStatus()
    {
        $data = self::where('status',1)->select(['id','status','end_time'])->get();
        foreach ($data as $item){
            if ($item['end_time'] < date("Y-m-d H:i:s")) {
                $item->status = 2;
                $item->save();
            }
        }
    }


}